package com.auth.manage.controller.user;

import cn.dev33.satoken.stp.StpUtil;
import com.auth.manage.domain.service.AuthorizationPortService;
import com.auth.manage.param.user.AuthPortAddParam;
import com.auth.manage.param.user.AuthPortDeleteParam;
import com.auth.manage.param.user.AuthPortUpdateParam;
import com.auth.manage.web.vo.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user/port")
@RequiredArgsConstructor
public class UserAuthorizationPortController {
    private final AuthorizationPortService authorizationPortService;
    // 添加端口
    @PostMapping
    public Result addPort(@RequestBody @Validated AuthPortAddParam param){
        authorizationPortService.addPort(param.getPortNum(), param.getPriority(), param.getCdk(), (Long) StpUtil.getLoginId());
        return Result.success();
    }
    // 删除端口
    @DeleteMapping
    public Result deletePort(@RequestBody @Validated AuthPortDeleteParam param){
        // 先看有没有传主键id，有则根据主键id删除
        if (param.getAuthCodeId() != null){
            boolean flag = authorizationPortService.deleteByAuthCodeId(param.getAuthCodeId(), param.getPortIds());
            return Result.builder().success(flag).build();
        }
        boolean flag = authorizationPortService.deleteByAuthCode(param.getAuthCodeCdk(), param.getPortIds());
        return Result.builder().success(flag).build();
    }
    // 修改端口号
    @PutMapping
    public Result updatePort(@RequestBody @Validated AuthPortUpdateParam param){
        authorizationPortService.updatePort(StpUtil.getLoginIdAsLong(), param.getOriginalPortId(), param.getPort());
        return Result.success();
    }


}
